<?php
	
	namespace Aspic\Form\Element;
	use Aspic\Security\Security as S;
	use Aspic\UString;
	use Aspic as A;
	
	/**
	* Group of radio form element
	* 
	* Radio group items are set in a span tag that regroup all the items
	* All attributes given in the options are set to the radioGroup tag
	*/
	class RadioGroup extends AbstractStandardElement {
		protected $radios;
		protected $_radiosSeparator;
		
		/**
		* @param array radios An array containing the radios elements. Keys are either 'label','value','labelClass','checked' or 0,1,2,3 (if checked is omitted, it is set to false)
		*/
		public function __construct($name, $groupLabel, array $radios = array(), array $options = array(), array $groupAttributes = array(), array $validations = array()) {
			parent::__construct($name, $groupLabel, $options, $groupAttributes, $validations);
			
			$this->setItems($radios);
			$this->opt('radiosSeparator', ' ', true);
		}
		
		public function setItems(array $radios = array()) {
			// Used to check if all fields are set and to escape fields that need it
			foreach($radios as $radio) {
				if(isset($radio[0])) {
					$this->addItem($radio[0], $radio[1], (isset($radio[2]))?$radio[2]:'', (isset($radio[3]))?$radio[3]:false);
				}
				else {
					$this->addItem(
						$radio['label'],
						A\Util::ifSet($radio['value'], $radio['label']),
						A\Util::ifSet($radio['checked'], false),  
						A\Util::ifSet($radio['attributes'], array())
						// ((isset($radio['attributes']))?$radio['attributes']:''),
						// (isset($radio['checked']))?$radio['checked']:false
					);
				}
			}
		}
		
		public function addItem($label, $value = null, $checked = false, array $attributes = array()) {
			$value = A\Util::ifSet($value, $label);
			
			$radio = array();

			$radio['label'] = $label;
			$radio['attributes'] = array_merge(array('value' => $value, 'checked' => (bool)$checked), $attributes);
			
			$this->radios[] = $radio;
			
			return $this;
		}
		
		/**
		* Set items from a 1-dimension array like: array('value1' => 'label1', 'value2' => 'label2')
		*/
		public function setKeyValueItems($items) {
			$parsedItems = array();
			
			foreach($items as $value => $label) {
				$item = array();
				
				$item['value'] = $value;
				$item['label'] = $label;
				
				$parsedItems[] = $item;
			}

			return $this->setItems($parsedItems);
		}
		
		public function outLabel() {
			$out = '<label>'.$this->_labelText.'</label>'; // = Element function without the for tag, which is useless here
		
			return $out;
		}
		
		public function outInput() {
			$arr_out = array();
			$out = '';
			
			if(count($this->radios)) {
				foreach($this->radios as $key => $radio) {
					if(!A\Util::getIfSet($radio['attributes']['checked'])) {
						unset($radio['attributes']['checked']);
					}
					else {
						$radio['attributes']['checked'] = '1';
					}
					
					$arr_out[] = '
						<input '.$this->getNameAttributeStr().' id="'.$this->getRadioId($radio['attributes']['value']).'" type="radio"'.UString::implodeHtmlAttributes($radio['attributes']).' />
						<label for="'.$this->getRadioId($radio['attributes']['value']).'">'.$radio['label'].'</label>
					';
				}
				
				$out = '<span'.$this->getAttrsStr().'>'.implode($this->_radiosSeparator, $arr_out).'</span>';
			}
			
			return $out;
		}
		
		protected function getDefaultId() {
			return 'rg'.ucfirst($this->getName());
		}
		
		protected function getRadioId($radioValue) {
			return 'radio'.ucfirst($this->getName()).'_'.$radioValue;
		}
		
		public function bind(array $formData = null) {
			if($value = $this->getValue($formData)) {
				foreach($this->radios as $key => &$it) {
					if($it['attributes']['value'] == $value) {
						$it['attributes']['checked'] = true;
					}
					else {
						$it['attributes']['checked'] = false;
					}
				}
			}
		}
		
	}	
	
?>